package org.servlet;/**
 * @author yang
 * @date 2024-03-07
 */

import com.google.gson.Gson;
import org.bean.ResponseData;
import org.bean.Staffs;
import org.dao.StaffsDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/GetStaffsServlet")
public class GetStaffsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        int id = Integer.parseInt(req.getParameter("id"));
        try {
            Staffs st = new StaffsDao().getStaffsbyid(id);
            ResponseData data = new ResponseData();
            data.setCode(200);
            data.setData(st);
            PrintWriter out = resp.getWriter();
            out.print(new Gson().toJson(data));
            out.flush();
            out.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
